package com.lagou.web;

import com.lagou.entity.Result;
import com.lagou.service.AccountService;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.sql.SQLException;

public class AccountServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
//            //设置编码
//            req.setCharacterEncoding("UTF-8");
//            resp.setContentType("text/html;charset=utf-8");

            if (req.getMethod().equalsIgnoreCase("post"))
                req.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=utf-8");

            // 获取请求的URI地址信息
            String url = req.getRequestURI();
            System.out.println("请求的URI地址信息: " + url);
            // 截取其中的方法名
            String methodName = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf(".action"));
            Method method = null;
            try {
                // 使用反射机制获取在本类中声明了的方法
                method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
                // 执行方法
                method.invoke(this, req, resp);
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("调用方法出错！");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void register(HttpServletRequest request, HttpServletResponse response) {

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        try {
            boolean flag = AccountService.register(username, password);
            response.sendRedirect("/homework23/account/");

        } catch (SQLException | IOException e) {
            e.printStackTrace();
        }
    }

    private void signIn(HttpServletRequest request, HttpServletResponse response) {

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        try {
            boolean flag = AccountService.signIn(username, password);

            String sessionId = request.getSession().getId();
            System.out.println("flag = " + flag);
            System.out.println("SessionId==" + sessionId);

            if (request.getSession().getAttribute("SessionId") == null)
                //将用户的名称保存到session属性中
                request.getSession().setAttribute("SessionId", sessionId);
            if (flag) {
//                //登录成功后，保存名称为JSESSIONID的id的cookie，值为session的id
//                Cookie c = new Cookie("JSESSIONID", sessionId);
//                c.setMaxAge(60 * 10);
//                c.setPath("/");
//                response.addCookie(c);
                response.getWriter().print(Result.toJsonString(1, "home.html", "登陆成功"));
            } else
                response.getWriter().print(Result.toJsonString(-99, "", "登录失败，用户名不存在或密码错误"));

        } catch (SQLException | IOException e) {
            e.printStackTrace();
        }
    }


}
